class VolunteersController < ApplicationController
  def new
    @volunteer =Volunteer.new
  end
  def create
    @volunteer = Volunteer.new(params[:volunteer])
    @volunteer.admit = 0
    @volunteer.service_time = 0
    user = User.find_by_id(session[:user_id])
    @volunteer.account = user.account
     respond_to do |format|
      if @volunteer.save
        format.html { redirect_to :controller=>"index" }
        format.xml  { render :xml => @volunteer, :status => :created, :location => @volunteer }
      else
        format.html { render :controller=>"volunteer", :action => "new" }
        format.xml  { render :xml => @volunteer.errors, :status => :unprocessable_entity }
      end
    end
   end

  
  def cognizancee
    @volunteers = Volunteer.find(:all,
      :conditions=> "admit = 0",
      :limit=>5)
    @page = 1
    render :file => "#{RAILS_ROOT}/app/views/volunteers/cognizancee.js.rjs"
  end

  def show
    @volunteer = Volunteer.find(params[:id])
    @user = User.find_by_account(@volunteer.account)
  end

   def agree
    @volunteer = Volunteer.find(params[:id])
    @volunteer.admit = 1
    @volunteer.save
    @uuser = User.find_by_account(@volunteer.account)
   # @uuser.permission = 2
    @uuser.update_attribute('permission', 2)
    @page = params[:page].to_i
    @volunteers = Volunteer.find(:all,
      :conditions=> "admit = 0",
      :limit=>5,
      :offset=>5*(@page-1))

    render :file => "#{RAILS_ROOT}/app/views/volunteers/agree.js.rjs"

  end
   def refuse
     user = User.find_by_id(params[:id])
     user.permission = 1
     user.save
     Volunteer.delete(params[:id])
    @page = params[:page].to_i
    @volunteers = Volunteer.find(:all,
      :conditions=> "admit = 0",
      :limit=>5,
      :offset=>5*(@page-1))

    render :file => "#{RAILS_ROOT}/app/views/volunteers/refuse.js.rjs"
  end
  def next_page
    @page = params[:page].to_i + 1
    @volunteers = Volunteer.find(:all,
      :conditions=> "admit = 0",
      :limit=>5,
      :offset=>5*(@page-1))
    render :file => "#{RAILS_ROOT}/app/views/volunteers/change_page.js.rjs"
  end

    def last_page
    @page = params[:page].to_i - 1
    @volunteers = Volunteer.find(:all,
      :conditions=> "admit = 0",
      :limit=>5,
      :offset=>5*(@page-1))
    render :file => "#{RAILS_ROOT}/app/views/volunteers/change_page.js.rjs"
  end


end